package com.cg.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cg.entity.FloorModel;
import com.cg.entity.GridAndBuildingVO;
import com.cg.entity.MapBuilding;
import com.cg.entity.MapGrid;
import com.cg.entity.UserInfo;
import com.cg.service.impl.MapBuildingService;
import com.cg.service.impl.MapGridService;
import com.cg.service.impl.UserInfoService;


@Controller
@RequestMapping("/admin/netmap/")
public class NetMapController {
    @Autowired
	private MapGridService mapGridService;
    
    @Autowired
    private UserInfoService userInfoService;
    
    @Autowired
    private MapBuildingService mapBuildingService;
	
    @RequestMapping("mainPage")
    public String mainPage() {
        return "netmap/map/main";
    }


    @RequestMapping("detailPage")
    public String detailPage() {
        return "netmap/map/detail";
    }
    
    @RequestMapping("allMapGrid")
    @ResponseBody
    public List<MapGrid> allMapGrid(){
   	 return mapGridService.getAllMapGrid();
    }
    
    @RequestMapping("allMapBuilding")
    @ResponseBody
    public List<MapBuilding> allMapBuilding(){
     	return mapBuildingService.getAll();
    }
    
    @RequestMapping("buildingUsers")
    @ResponseBody
    public FloorModel buildingUsers(@RequestParam("buildingId") int buildingId,Map<String,Object> models){
        return userInfoService.queryUserByBuilding(buildingId);
        
    }
       
    @RequestMapping("buildingUser")
    @ResponseBody
    public UserInfo buildingUser(@RequestParam("buildingId") int buildingId,@RequestParam("buildingAdress") String buildingAdress, 
    		@RequestParam("name") String name
    		,Map<String,Object> models){
     	 List<UserInfo> userInfos = userInfoService.queryUserByAdress(buildingId, buildingAdress);
     	 List<String> names = new ArrayList<String>();
     	 UserInfo currentUser = new UserInfo();
     	 if(null != userInfos && !userInfos.isEmpty()) {
     		 for(UserInfo info : userInfos) {
     			 names.add(info.getUserName()); 
     			 if(StringUtils.isNotEmpty(name)&& name.equals(info.getUserName())) {
     				currentUser=info;
         		 }
     		 }
     		 if(StringUtils.isEmpty(name)) {
     			currentUser=userInfos.get(0);
     		 }
     	 }
     	 currentUser.setUserNames(names);
     	 return currentUser;
    }
    
    @RequestMapping("mapshearch")
    @ResponseBody
    public GridAndBuildingVO mapshearch(@RequestParam("userName")String userName){
      	return userInfoService.queryUsersforMap(userName);
    }
    
    @RequestMapping("mapoutpolygon")
    @ResponseBody
    public List<MapGrid> mapoutpolygon(){
     	return mapGridService.getPolygon();
    }
   
    
}
